package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * ShoppingCartService
 *
 * @author liudo
 * @version 1.0
 * @project sky-take-out
 * @date 2024/2/23 12:07:28
 */
@Service
@RequiredArgsConstructor
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private final ShoppingCartMapper shoppingCartMapper;

    private final DishMapper dishMapper;

    private final SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        Long currentId = BaseContext.getCurrentId();
        if (Objects.nonNull(shoppingCartDTO.getDishId())) {
            // 当前的 甜大白菜是否存在
            ShoppingCart shoppingCartQuery = new ShoppingCart();
            shoppingCartQuery.setDishId(shoppingCartDTO.getDishId());
            shoppingCartQuery.setDishFlavor(shoppingCartDTO.getDishFlavor());
            ShoppingCart shoppingCart = shoppingCartMapper.selectOne(shoppingCartQuery);

            if (Objects.isNull(shoppingCart)) {
                // 不存在菜品 新增一条记录
                Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                shoppingCart = new ShoppingCart();
                shoppingCart.setName(dish.getName());
                shoppingCart.setUserId(currentId);
                shoppingCart.setDishId(shoppingCartDTO.getDishId());
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            } else {
                // 存在则直接更新数量
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                shoppingCartMapper.updateById(shoppingCart);
            }

        }

        if (Objects.nonNull(shoppingCartDTO.getSetmealId())) {
            // 套餐
            ShoppingCart shoppingCartQuery = new ShoppingCart();
            shoppingCartQuery.setSetmealId(shoppingCartDTO.getSetmealId());
            shoppingCartQuery.setDishFlavor(shoppingCartDTO.getDishFlavor());
            ShoppingCart shoppingCart = shoppingCartMapper.selectOne(shoppingCartQuery);

            if (Objects.isNull(shoppingCart)) {
                // 不存在套餐 新增一条记录
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart = new ShoppingCart();
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setUserId(currentId);
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            } else {
                // 存在则直接更新数量
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                shoppingCartMapper.updateById(shoppingCart);
            }
        }
    }

    @Override
    public List<ShoppingCart> listByUserId(Long userId) {
        ShoppingCart shoppingCartQuery = new ShoppingCart();
        shoppingCartQuery.setUserId(userId);
        return shoppingCartMapper.selectList(shoppingCartQuery);
    }

    @Override
    public void cleanByUserId(Long userId) {
        shoppingCartMapper.deleteByUserId(userId);
    }
}
